package com.spa.application.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.spa.application.command.admin.ShopAddCommand;
import com.spa.application.command.admin.ShopEditCommand;
import com.spa.application.command.admin.ShopUserAddCommand;
import com.spa.application.command.shop.ApplyRefundCommand;
import com.spa.application.command.shop.MassagistEvaluateDelCommand;
import com.spa.application.command.shop.OrderOperTransferCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.AllShopDTO;
import com.spa.application.dto.admin.ShopDTO;
import com.spa.application.dto.admin.ShopExportDTO;
import com.spa.application.dto.admin.ShopMaxRuleDTO;
import com.spa.application.dto.common.rule.RuleDto;
import com.spa.application.dto.shop.*;
import com.spa.application.query.admin.ShopQuery;
import com.spa.application.query.shop.*;
import com.spa.domain.entity.*;
import com.spa.domain.service.*;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.enums.dict.massagist.MassagistStatusEnum;
import com.spa.infrastructure.enums.dict.massagist.MassagistTypeEnum;
import com.spa.infrastructure.enums.dict.order.OrderStatusEnum;
import com.spa.infrastructure.enums.dict.order.OrderTransferStatusEnum;
import com.spa.infrastructure.enums.dict.refund.ApplyRefundStatusEnum;
import com.spa.infrastructure.enums.dict.rule.RuleClassifyEnum;
import com.spa.infrastructure.enums.dict.rule.RuleFlagEnum;
import com.spa.infrastructure.enums.dict.rule.RuleTypeEnum;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.mapper.*;
import com.spa.infrastructure.repository.condition.*;
import com.spa.infrastructure.repository.po.ApplyRefundPO;
import com.spa.infrastructure.repository.po.MassagistEvaluatePO;
import com.spa.infrastructure.service.mp.FlowEnum;
import com.spa.infrastructure.service.mp.Producer;
import com.spa.infrastructure.service.sms.ApplicationConfiguration;
import com.spa.infrastructure.service.yunxin.VoiceSenderService;
import com.spa.infrastructure.util.SessionUtil;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ShopApplicationService {

    @Resource
    private ShopService shopService;

    @Resource
    private RuleService ruleService;

    @Resource
    private ShopUserService shopUserService;

    @Resource
    private MassagistService massagistService;

    @Resource
    private MassageService massageService;

    @Resource
    private MassagistEvaluateService massagistEvaluateService;

    @Resource
    private FeedbackService feedbackService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderTransferService orderTransferService;

    //默认提现周期（天）
    private Integer WITHDRAW_PERIOD = 30;

    @Autowired
    private Producer producer;

    @Autowired
    private VoiceSenderService voiceSenderService;

    @Autowired
    private ApplicationConfiguration smsConfig;

    @Autowired
    private SysAdminUserService sysAdminUserService;

    @Autowired
    private RefundDomainService refundDomainService;

    @Autowired
    private ProfitService profitService;

    @Transactional
    public void addShop(ShopAddCommand command) {
        //校验门店是否存在
        Shop shop = shopService.getShopByName(command.getName());
        if (ObjectUtil.isNotNull(shop)) {
            throw new SpaException("门店已经存在");
        }
        Long shopId = shopService.addShop(ShopMapperStruct.INSTANCE.command2Po(command));
        ShopUserAddCommand shopUserAddCommand = new ShopUserAddCommand();
        shopUserAddCommand.setShopId(shopId);
        shopUserAddCommand.setPhone(command.getAccount());
        shopUserAddCommand.setName(command.getOwner());
        Integer withdrawPeriod = command.getWithdrawPeriod();
        if (ObjectUtils.isEmpty(command.getWithdrawPeriod())) {
            withdrawPeriod = WITHDRAW_PERIOD;
        }
        shopUserAddCommand.setWithdrawPeriod(withdrawPeriod);
        shopUserService.addShopUser(shopUserAddCommand, WhetherTypeEnum.$0);


        //添加规则
        ruleService.addRule(command.getRuleDtoList(), shopId, RuleClassifyEnum.$20);

    }

    @Transactional
    public void editShop(ShopEditCommand command) {
        shopService.editShop(command);
        if (CollUtil.isNotEmpty(command.getRuleDtoList())) {

            ShopMaxRuleDTO shopMassagistMaxRuleDTO = massagistRuleMaxVal(Convert.toLong(command.getId()));
            List<RuleDto> tempList1 = Rule.listRuleParamToDto(command.getRuleDtoList(), RuleClassifyEnum.$20);

            List<RuleDto> tempList2 = new ArrayList<>();
            tempList2.addAll(shopMassagistMaxRuleDTO.getOrderRuleList());
            tempList2.addAll(shopMassagistMaxRuleDTO.getOrderExtRuleList());

            //校验门店下技师的规则是否超额
            Rule.verifyBoundary(tempList1, tempList2);

            //添加规则
            ruleService.addRule(command.getRuleDtoList(), Convert.toLong(command.getId()), RuleClassifyEnum.$20);
        }
    }

    public PageResult<ShopDTO> pageShop(ShopQuery query) {
        Page<Shop> ipage = shopService.pageShop(query);
        return ShopMapperStruct.INSTANCE.entity2ShopDto4Page(ipage);
    }

    public ShopDTO detail(ShopQuery query) {
        Shop shop = shopService.getShopById(NumberUtil.parseLong(query.getId()));
        ShopDTO shopDTO = ShopMapperStruct.INSTANCE.entity2ShopDto(shop);
        RuleCondition ruleCondition = new RuleCondition();
        ruleCondition.setRelevanceId(shop.getId());
        List<RuleDto> ruleDtoList = Rule.listRuleToDto(ruleService.listRule(ruleCondition));
        shopDTO.setRuleDtoList(ruleDtoList);
        return shopDTO;
    }

    public void offLine(Long id) {
        shopService.offlineShop(id);
    }

    public void onLine(Long id) {
        shopService.onlineShop(id);
    }

    public List<ShopMassagistCountDTO> shopMassagistCount() {
        List<ShopMassagistCountDTO> dtoList = new ArrayList<>();
        List<Massagist> massagistList = massagistService.getMassagistListByShopId(SessionUtil.getUser().getShopId());
        for (MassagistStatusEnum e : MassagistStatusEnum.values()) {
            ShopMassagistCountDTO dto = new ShopMassagistCountDTO();
            dto.setCount(massagistList.stream().filter(f -> Objects.equals(f.getStatus(), e.getCode())).count());
            dto.setStatusCode(e.getCode());
            dto.setStatusDesc(e.getDesc());
            dtoList.add(dto);
        }
        return dtoList;
    }

    public List<ShopServiceDTO> serviceList() {
        MassageServiceCondition condition = new MassageServiceCondition();
        condition.setIsDel(WhetherTypeEnum.$0.getCode());
        return MassageServiceMapperStruct.INSTANCE.entity2ShopDto4List(massageService.list(condition));
    }

    public ShopMassagistDetailsDTO massagistInfoById(ShopMassagistDetailsQuery query) {
        Massagist massagist = massagistService.infoById(query.getId());
        ShopMassagistDetailsDTO dto = MassagistMapperStruct.INSTANCE.entity2ShopDto(massagist);
        List<ShopServiceDTO> shopServiceDTOS = MassageServiceMapperStruct.INSTANCE.entity2ShopDto4List(massageService.serviceListByMassagistId(massagist.getId()));
        //门店信息
        Shop shop = shopService.getInfoById(massagist.getShopId());
        dto.setServiceList(shopServiceDTOS);
        dto.setShopId(shop.getId());
        dto.setShopName(shop.getName());

        //技师规则
        RuleCondition ruleCondition = new RuleCondition();
        ruleCondition.setRelevanceId(massagist.getId());
        ruleCondition.setClassify(RuleClassifyEnum.$10.getCode());
        List<Rule> ruleDtoList = ruleService.listRule(ruleCondition);
        if (CollUtil.isNotEmpty(ruleDtoList)) {
            dto.setRuleDtoList(Rule.listRuleToDto(ruleDtoList));
        }

        return dto;

    }

    public ShopMassagistEvaluateStatisticsDTO massagistEvaluateStatistics() {
        ShopMassagistEvaluateStatisticsDTO statisticsDTO = new ShopMassagistEvaluateStatisticsDTO();
        //门店ID
        Long shopId = SessionUtil.getUser().getShopId();
        Assert.isFalse(ObjectUtil.isNull(shopId), "获取门店信息失败");
        List<Massagist> massagistList = massagistService.getMassagistListByShopId(shopId);
        Map<String, Long> commType = new HashMap<>();
        if (CollUtil.isNotEmpty(massagistList)) {
            List<Long> mIds = massagistList.stream().map(Massagist::getId).toList();
            MassagistEvaluateCondition evaluateCondition = new MassagistEvaluateCondition();
            evaluateCondition.setIsDel(WhetherTypeEnum.$0.getCode());
            evaluateCondition.setMassagistIds(mIds);
            List<MassagistEvaluate> meList = massagistEvaluateService.listInfo(evaluateCondition);
            if (CollUtil.isNotEmpty(meList)) {
                commType.put("allNum", Convert.toLong(meList.size(), 0l));
                //门店评价数
                statisticsDTO.setTotal(Convert.toStr(meList.size(), "0"));
                //门店好评数
                Long goodNum = meList.stream().filter(me -> Convert.toInt(me.getScore(), 0) >= 4 && Convert.toInt(me.getScore(), 0) <= 5).count();
                commType.put("goodNum", Convert.toLong(goodNum, 0l));
                //门店中评数
                Long middleNum = meList.stream().filter(me -> Convert.toInt(me.getScore(), 0) >= 3 && Convert.toInt(me.getScore(), 0) < 4).count();
                commType.put("middleNum", Convert.toLong(middleNum, 0l));
                //门店差评数
                Long badNum = meList.stream().filter(me -> Convert.toInt(me.getScore(), 0) >= 0 && Convert.toInt(me.getScore(), 0) < 3).count();
                commType.put("badNum", Convert.toLong(badNum, 0l));

            }
        }

        if (CollUtil.isEmpty(commType)) {
            commType.put("allNum", 0l);
            statisticsDTO.setTotal("0");
            commType.put("goodNum", 0l);
            commType.put("middleNum", 0l);
            commType.put("badNum", 0l);
        }

        statisticsDTO.setCommType(commType);
        statisticsDTO.setShopScore(shopService.getShopScore(massagistList));
        return statisticsDTO;
    }

    public PageResult<ShopMassagistEvaluateDTO> massagistEvaluatePage(ShopMassagistEvaluateQuery query) {
        Long shopId = SessionUtil.getUser().getShopId();
        Assert.isFalse(ObjectUtil.isNull(shopId), "未获取到门店信息");
        List<Long> mIdList = massagistService.getMassagistListByShopId(SessionUtil.getUser().getShopId()).stream().map(Massagist::getId).toList();
        MassagistEvaluateCondition condition = new MassagistEvaluateCondition();
        condition.setCurrent(query.getCurrent());
        condition.setSize(query.getSize());
        condition.setIsDel(WhetherTypeEnum.$0.getCode());
        if (CollUtil.isNotEmpty(mIdList)) {
            condition.setMassagistIds(mIdList);
            if (ObjectUtil.isNotNull(query.getEvaluate())) {
                switch (query.getEvaluate()) {
                    case 10:
                        condition.setInScore((List<Integer>) Convert.toList("1,2"));
                        break;
                    case 20:
                        condition.setInScore((List<Integer>) Convert.toList("3"));
                        break;
                    case 30:
                        condition.setInScore((List<Integer>) Convert.toList("4,5"));
                        break;
                }
            }
            Page<MassagistEvaluate> massagistEvaluatePage = massagistEvaluateService.pageListInfo(condition);
            PageResult<ShopMassagistEvaluateDTO> dtoPageResult = MassagistEvaluateMapperStruct.INSTANCE.entity2ShopDto4Page(massagistEvaluatePage);
            return dtoPageResult;
        }
        return new PageResult<>();
    }

    /**
     * 删除 技师评价信息
     *
     * @author: lig
     * @date: 2024/1/9
     */
    @Transactional
    public boolean massagistEvaluateDel(MassagistEvaluateDelCommand command) {
        //评价是否存在
        MassagistEvaluatePO po = massagistEvaluateService.infoById(command.getId());
        Assert.isFalse(BeanUtil.isEmpty(po), "信息不存在");
        if (massagistEvaluateService.updateDelById(po.getId(), WhetherTypeEnum.$1)) {
            //更新技师评分
            massagistEvaluateService.updateMassagistEvaluate(po.getMassagistId());
            return true;
        }
        return false;
    }

    /**
     * 反馈/投诉 分页列表
     *
     * @author: lig
     * @date: 2024/1/13
     */
    public PageResult<ShopFeedbackDTO> shopFeedbackPage(ShopFeedbackQuery query) {
        Long shopId = SessionUtil.getUser().getShopId();
        Assert.isFalse(ObjectUtil.isNull(shopId), "未获取到门店唯一信息");
        Shop shop = shopService.getInfoById(shopId);
        Assert.isFalse(ObjectUtil.isNull(shop), "未获取到门店信息");
        FeedbackCondition condition = FeedbackMapperStruct.INSTANCE.shopQuery2Condition(query);
        condition.setCityId(shop.getCityId());
        Page<Feedback> page = feedbackService.pageListInfo(condition);
        return FeedbackMapperStruct.INSTANCE.entity2ShopDto4Page(page);
    }

    public void orderTransferReceiv(OrderOperTransferCommand command) {
        //订单是否未接单
        OrderInfoCondition condition = new OrderInfoCondition();
        condition.setOrderId(Long.parseLong(command.getOrderId()));
        condition.setShopId(SessionUtil.getUser().getShopId());
        condition.setStatus(OrderStatusEnum.$20.getCode());
        if (!orderService.existsByCondition(condition)) {
            throw new SpaException("转单失败，不存在待接单的订单");
        }
        Massagist massagist = massagistService.getInfoByMassagistId(Long.parseLong(command.getMassagistId()));
        if (ObjectUtil.equals(massagist.getTag(), MassagistTypeEnum.$10.getCode())) {
            OrderTransferCondition orderTransferCondition = new OrderTransferCondition();
            orderTransferCondition.setOrderId(Long.parseLong(command.getOrderId()));
            orderTransferCondition.setHandleMark(1);
            List<OrderTransfer> orderTransfers = orderTransferService.list4GroupByOrderLimitOne(orderTransferCondition);
            if (CollUtil.isEmpty(orderTransfers)) {
                throw new SpaException("转单失败，该订单已超时自动转单");
            }
            orderService.orderTransfer2Massagist(Long.parseLong(command.getOrderId()), massagist);
            //发送通知给技师接单
            producer.send(FlowEnum.$PLACE, Long.parseLong(command.getOrderId()));
            //语音通知
            if (smsConfig.isFlagSendVoiceSms()) {
                LinkedHashMap<String, String> templateArgs = new LinkedHashMap<>();
                templateArgs.put("var1", command.getOrderId());
                try {
                    voiceSenderService.send(massagist.getPhone(), templateArgs, "402502");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public Long queryOrderTransferCount(OrderTransferCountQuery query) {
        return ObjectUtil.defaultIfNull(orderTransferService.countByShopId(SessionUtil.getUser().getShopId(), OrderTransferStatusEnum.$10), 0L);
    }

    public Page<OrderTransferDto> queryOrderTransfer(OrderTransferCountQuery query) {
        OrderTransferCondition condition = OrderTransferMapperStruct.INSTANCE.query2Condition(query);
        condition.setShopId(SessionUtil.getUser().getShopId());
//        condition.setHandleMark(1);
        Page<OrderTransfer> orderTransfers = orderTransferService.page4GroupByOrderLimitOne(condition);
        return OrderTransferMapperStruct.INSTANCE.entity2Dto4List(orderTransfers);
    }

    public List<ShopExportDTO> shopList(ShopQuery query) {
        return ShopMapperStruct.INSTANCE.entity2ShopExportDto(shopService.shopList(query));
    }

    public List<AllShopDTO> getAllShop() {
        List<Long> shopIds = sysAdminUserService.getMyDataPermission();
        if (CollUtil.isNotEmpty(shopIds)) {
            return ShopMapperStruct.INSTANCE.entity2AllShopDTO(shopService.listShopByIds(shopIds));
        }
        return ShopMapperStruct.INSTANCE.entity2AllShopDTO(shopService.listAllShop());
    }

    public String callUser(long orderId) throws Exception {
        String phone = SessionUtil.getUser().getShopUser().getPhone();
        ShopUser shopUser = shopUserService.getShopUserByPhone(phone);
        if (StringUtils.isNotEmpty(shopUser.getOwnerPhone())) {
            phone = shopUser.getOwnerPhone();
        }
        OrderInfo orderInfo = orderService.getOrderInfoById(orderId, OrderService.OrderInfoPlug.empty());
        return voiceSenderService.bind(orderInfo.getUserPhone(), phone);
    }

    public ShopMaxRuleDTO massagistRuleMaxVal(Long shopId) {
        shopService.getShopById(shopId);
        List<Massagist> massagistList = massagistService.listByShopId(shopId);
        ShopMaxRuleDTO result = new ShopMaxRuleDTO();
        List<RuleDto> orderRuleList = new ArrayList<>();
        List<RuleDto> orderExtRuleList = new ArrayList<>();

        if (CollUtil.isNotEmpty(massagistList)) {
            List<Long> massagistIdList = massagistList.stream().map(Massagist::getId).toList();

            RuleCondition ruleCondition = new RuleCondition();
            ruleCondition.setRelevanceIds(massagistIdList);
            ruleCondition.setClassify(RuleClassifyEnum.$10.getCode());
            List<Rule> ruleList = ruleService.listRule(ruleCondition);

            orderRuleList = buildOrderMax(ruleList);
            orderExtRuleList = buildOrderExtMax(ruleList);

        }

        result.setOrderRuleList(orderRuleList);
        result.setOrderExtRuleList(orderExtRuleList);

        return result;


    }

    /**
     * 查询门店规则列表
     *
     * @author: lig
     * @date: 2024/4/10
     */
    public ShopMaxRuleDTO queryShopRuleList(Long massagistId) {
        ShopMaxRuleDTO result = new ShopMaxRuleDTO();
        List<RuleDto> orderRuleList = new ArrayList<>();
        List<RuleDto> orderExtRuleList = new ArrayList<>();

        Massagist massagist = massagistService.infoById(massagistId);
        if (BeanUtil.isEmpty(massagist)) {
            throw new SpaException("技师不存在");
        }
        Long shopId = massagist.getShopId();
        shopService.getShopById(shopId);
        RuleCondition ruleCondition = new RuleCondition();
        ruleCondition.setRelevanceId(shopId);
        ruleCondition.setClassify(RuleClassifyEnum.$20.getCode());
        List<RuleDto> ruleDtoList = Rule.listRuleToDto(ruleService.listRule(ruleCondition));
        orderRuleList = ruleDtoList.stream().filter(f1 -> RuleFlagEnum.$10.getDesc().equals(f1.getRuleFlagDesc())).toList();
        orderExtRuleList = ruleDtoList.stream().filter(f1 -> RuleFlagEnum.$20.getDesc().equals(f1.getRuleFlagDesc())).toList();
        result.setOrderRuleList(orderRuleList);
        result.setOrderExtRuleList(orderExtRuleList);

        return result;


    }


    private List<RuleDto> buildOrderMax(List<Rule> ruleList) {

        List<RuleDto> list = new ArrayList<>();

        //订单
        List<Rule> orderRuleList1 = ruleList.stream().filter(f1 -> RuleFlagEnum.$10.getCode().equals(f1.getFlag())).toList();
        if (CollUtil.isEmpty(orderRuleList1)) {
            return list;
        }

        //固定规则
        List<Rule> fixedRuleList = orderRuleList1.stream().filter(f1 -> RuleTypeEnum.$10.getCode().equals(f1.getType())).toList();
        if (CollUtil.isNotEmpty(fixedRuleList)) {
            //固定规则最大值
            Rule fixedMaxRule = fixedRuleList.stream().max(Comparator.comparing(Rule::getVal)).get();
            list = Rule.listRuleToDto(Arrays.asList(fixedMaxRule));
        }

        //浮动规则
        List<Rule> floatRuleList = orderRuleList1.stream().filter(f1 -> RuleTypeEnum.$20.getCode().equals(f1.getType())).toList();
        list = buildFloatRuleMax(floatRuleList);


        return list;

    }

    private List<RuleDto> buildOrderExtMax(List<Rule> ruleList) {
        List<RuleDto> list = new ArrayList<>();
        //加钟
        List<Rule> orderExtRuleList1 = ruleList.stream().filter(f1 -> RuleFlagEnum.$20.getCode().equals(f1.getFlag())).toList();
        list = buildFloatRuleMax(orderExtRuleList1);

        return list;

    }

    private List<RuleDto> buildFloatRuleMax(List<Rule> ruleList) {
        Map<Integer, List<Rule>> groupedItems = ruleList.stream()
                .collect(Collectors.groupingBy(Rule::getSerial));
        List<Rule> tempList = new ArrayList<>();
        groupedItems.forEach((key, val) -> {
            //浮动规则最大值
            Rule r = val.stream().max(Comparator.comparing(Rule::getVal)).get();
            tempList.add(r);
        });
        return Rule.listRuleToDto(tempList);
    }

    /**
     * 此功能不支持对加钟进行退款
     * @param command
     */
    @Transactional
    public void applyRefund(ApplyRefundCommand command) {
        //校验退款金额是否合理
        if (command.getPrice() <= 0) {
            throw new SpaException("无效的退款金额");
        }
        ApplyRefundCondition condition = new ApplyRefundCondition();
        condition.setStatusList(Lists.newArrayList(ApplyRefundStatusEnum.$10.getCode(), ApplyRefundStatusEnum.$20.getCode(), ApplyRefundStatusEnum.$40.getCode()));
        ApplyRefund applyRefund = refundDomainService.getApplyRefund(condition);
        if (ObjectUtil.isNotNull(applyRefund)) {
            throw new SpaException("不能重复发起退款");
        }
        OrderInfo orderInfo = orderService.getOrderInfoById(command.getOrderId(), OrderService.OrderInfoPlug.empty());
        int servicePrice = orderInfo.getOrderPrice() - orderInfo.getTravelPrice();
        if (command.getPrice() > servicePrice) {
            throw new SpaException("退款金额不得大于订单支付金额");
        }
        //生成退款记录
        ApplyRefundPO po = ApplyRefundMapperStruct.INSTANCE.command2Po(command);
        po.setActualPrice(orderInfo.getOrderPrice());
        if (orderInfo.getStatus().equals(OrderStatusEnum.$99.getCode())) {
            //取消单进行退款，只涉及到平台
            po.setAdminPrice(command.getPrice());
        } else if (orderInfo.getStatus().equals(OrderStatusEnum.$40.getCode())) {
            //完成订单退款，扣款次序为：技师---》门店---》平台（只查询订单的分润）
            Profit profit = profitService.queryProfitByOrderIdAndType(command.getOrderId(), 1);
            int massagistIncome = profit.getMassagistIncome();
            int shopIncome = profit.getShopIncome();
            int adminIncome = profit.getAdminIncome();
            //申请退款的金额小于等于技师的收入， 只扣除技师的业绩
            if (command.getPrice() <= massagistIncome) {
                po.setMassagistPrice(command.getPrice());
            } else if (command.getPrice() <= massagistIncome + shopIncome) {
                po.setMassagistPrice(massagistIncome);
                po.setShopPrice(command.getPrice() - massagistIncome);
            } else {
                po.setMassagistPrice(massagistIncome);
                po.setShopPrice(shopIncome);
                po.setAdminPrice(command.getPrice() - massagistIncome - shopIncome);
            }
        }
        po.setOrderCode(orderInfo.getOrderCode());
        po.setApplyId(SessionUtil.getUser().getExtUserId());
        po.setApplyName(SessionUtil.getUser().getShopUser().getName());
        if (command.getRefundTravelPriceFlag()) {
            po.setTravelPrice(orderInfo.getTravelPrice());
        }
        refundDomainService.add(po);
    }

    public PageResult<ApplyRefundRecordDTO> queryApplyRefundList(ApplyRefundListQuery query) {
        Page<ApplyRefund> applyRefundPage = refundDomainService.pageApplyRefund(ApplyRefundMapperStruct.INSTANCE.query2Condition(query));
        return ApplyRefundMapperStruct.INSTANCE.entity2DtoPage(applyRefundPage);
    }
}
